package com.weijin.springbootbase.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/11 11:00
 */
@Data
@ApiModel("通用响应体类")
public class Result<T> implements Serializable {

    @ApiModelProperty("响应码")
    private Integer code;
    @ApiModelProperty("响应内容")
    private T data;
    @ApiModelProperty("响应提示信息")
    private String msg;
    @ApiModelProperty("响应时间戳")
    private Long timestamp;

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(String msg, T data) {
        return result(1, msg, data);
    }

    public static <T> Result<T> success(String msg) {
        return result(1, msg, null);
    }

    public static <T> Result<T> failed(String msg) {
        return result(0, msg, null);
    }

    public static <T> Result<T> failed(Integer code, String msg) {
        return result(code, msg, null);
    }

    private static <T> Result<T> result(Integer code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }
}
